import sensor, image, time, math
from pyb import UART
import json
import ustruct
red_threshold_01 =(40, 15, 61, 17, 55, -39)
green_threshold_01 =(100, 83, -126, 127, 127, -128)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_auto_gain(False)
sensor.skip_frames(time=2000)
sensor.set_auto_exposure(False, 8000)
sensor.set_auto_whitebal(False)
clock = time.clock()
uart = UART(3,115200)
uart.init(115200, bits=8, parity=None, stop=1)
ROI =[138,117,31,27]
rio=(138,117,31,27)


clock = time.clock()

while(True):
    x=0;y=0
    clock.tick()
    img = sensor.snapshot()
    #print(clock.fps())
    blobs = img.find_blobs([red_threshold_01],roi=ROI)
    blobd = img.find_blobs([green_threshold_01], roi=ROI)
    img.draw_rectangle(rio,color=(255,0,0))
    if blobs:
        x=1
    if blobd:
        y=1
    data = bytearray([0xb3,x,y,0x5b])
    uart.write(data)
    print(x,y)
uart.deinit()       
      